﻿

//省市区联动
if( CitySel!= undefined ){
    CitySel.init('Order_province', 'Order_city', 'Order_district');
	$('#showSelect').click(function(){$('#showValue').html(CitySel.getTxt()+'<br/>'+CitySel.getVal());});
}


function getRadioValue(RadioName) {
    var radionObj = document.getElementsByName(RadioName);
    if (radionObj == undefined) {
        return "";
    }
    for (var i = 0; i < radionObj.length; i++) {
        if (radionObj[i].checked) {
            str = radionObj[i].value;
        }
    }

    return str;
}

function getTextValue(ObjID) {
    var Obj = document.getElementById(ObjID);
    if ( Obj == undefined) {
        return "";
    }
    if (Obj.type == "text") {
        return Obj.value;
    }
    
    if (Obj.type == "select-one") {
        return Obj.options[Obj.selectedIndex] == undefined ? "" : Obj.options[Obj.selectedIndex].text;
    }
    
}

function setFocus(ObjID) {
   if(document.getElementById(ObjID) != undefined) {
       document.getElementById(ObjID).focus();
    }
}

//youtianjifu order
function ordersubmit() {
    //testisNum(document.getElementById);
    var reg=/^\d+$/;
    if (!reg.test(getTextValue("Quantity"))) {
        alert("订购数量只能是数字！");
        setFocus("Quantity");
        return false;
    }
    if ((getTextValue("Receiver")) == "" ) {
        alert("订购人姓名不能为空！");
        setFocus("Receiver");
        return false;
    }
    if ((getTextValue("ReceiverAddress")) == "") {
        alert("详细地址不能为空！");
        setFocus("ReceiverAddress");
        return false;
    }
    if ((getTextValue("Mobile")) == "") {
        alert("手机不能为空！");
        setFocus("Mobile");
        return false;
    } else {
        if (getTextValue("Mobile").toString().length<8 || !reg.test(getTextValue("Mobile"))) {
            alert("请输入正确的手机号码！");
            setFocus("Mobile");
            return false;
        }
    }

    if ((getTextValue("Postcode")) != "") {
        var s = document.getElementById("Postcode").value;
        var pattern = /^[0-9]{6}$/;
        if (s != "") {
            if (!pattern.exec(s)) {
                alert('请输入正确的邮政编码');
                setFocus("Postcode");
                return false;
            }
        }
        
    }
   

//    if ((getTextValue("ReceiverTel")) == "") {
//        alert("固定电话不能为空！");
//        setFocus("ReceiverTel");
//        return false;
//    }
    var url = "Handler.ashx?action=order";
    url = url + "&ProductStruct=" + getRadioValue("ProductStruct");
    url = url + "&Quantity=" + getTextValue("Quantity");
    url = url + "&Receiver=" + getTextValue("Receiver");
    url = url + "&Province=" + getTextValue("province");
    url = url + "&City=" + getTextValue("city");
    url = url + "&District=" + getTextValue("district");
    url = url + "&ReceiverAddress=" + getTextValue("province") + getTextValue("city") + getTextValue("district") + getTextValue("ReceiverAddress");
    url = url + "&Postcode=" + getTextValue("Postcode");
    url = url + "&Mobile=" + getTextValue("Mobile");
    url = url + "&ReceiverTel=" + getTextValue("ReceiverTel");
    url = url + "&PayModeRemark=" + getRadioValue("PayModeRemark");
    url = url + "ShipDateRemark&=" + getRadioValue("ShipDateRemark");
    url = url + "&Remark=" + getTextValue("Remark");
    PostData(url);
    alert("提交成功！");
    return false;
}


function GetQueryString(name, urlParam) {

    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = urlParam.match(reg);
    if (r != null) {
        return unescape(r[2]);
    }

    return null;

}


function PostData(url) {
    var xmlhttp = false;
    try {
        xmlhttp = new XMLHttpRequest();
    }
    catch (trymicrosoft) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed) {
                xmlhttp = false;
            }
        }
    }

    if (!xmlhttp) {
        alert("Error initializing XMLHttpRequest!");
    }

    xmlhttp.open("get", url, false); //true代表异步传输，结果在onreadystatechange的回调函数中进行处理。
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
            }
        }
    }

    xmlhttp.send("");

}

//reset
function orderreset(){
    document.getElementById("form1").reset();
    return false;
}

//随机返回数组中的某个值
function CreateRecord(obj){
    return obj[Math.floor(Math.random()*obj.length)];
}

//取得当前日期 格式 yyyy-MM-dd
function GetNowTime(){
    var nowtime=new Date();
    var nowstr=nowtime.getFullYear()+"-";
    var nowmonth=nowtime.getMonth()+1;
    if(nowmonth<10){
        nowmonth="0"+nowmonth;
    }
    var nowdate=nowtime.getDate();
    if(nowdate<10){
        nowdate="0"+nowdate; 
    }
    nowstr+=nowmonth+"-"+nowdate;        
    return nowstr;
}



//数字
function testisNum(object) {

    var s = document.getElementById(object.id).value;

    if (s != "") {
        if (isNaN(s)) {
            alert("请输入数字");
            object.value = "";
            object.focus();
        }
    }
}

//电话号码，传真 校验普通电话、传真号码：可以“+”开头，除数字外，可含有“-”
function isTel(object) {
    //国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"

    var s = document.getElementById(object.id).value;
    var pattern = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
    //var pattern =/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/; 
    if (s != "") {
        if (!pattern.exec(s)) {
            alert('请输入正确的电话号码:电话号码格式为国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"');
            object.value = "";
            object.focus();
        }
    }
}

//邮箱
function Check(object) {
    var s = document.getElementById(object.id).value;
    var pattern = /^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$/;
    if (s != "") {
        if (!pattern.exec(s)) {
            alert('请输入正确的邮箱地址');
            object.value = "";
            object.focus();
        }
    }

}

//校验手机号码：必须以数字开头，除数字外，可含有“-”
function isMobile(object) {
    var s = document.getElementById(object.id).value;
    var reg0 = /^13\d{5,9}$/;
    var reg1 = /^153\d{4,8}$/;
    var reg2 = /^159\d{4,8}$/;
    var reg3 = /^0\d{10,11}$/;
    var my = false;
    if (reg0.test(s)) my = true;
    if (reg1.test(s)) my = true;
    if (reg2.test(s)) my = true;
    if (reg3.test(s)) my = true;
    if (s != "") {
        if (!my) {
            alert('请输入正确的手机号码');
            object.value = "";
            object.focus();
        }
    }
}

//校验(国内)邮政编码
function isPostalCode(object) {
    var s = document.getElementById(object.id).value;
    var pattern = /^[0-9]{6}$/;
    if (s != "") {
        if (!pattern.exec(s)) {
            alert('请输入正确的邮政编码');
            object.value = "";
            object.focus();
        }
    }
}

function getHost(url) {
    var host = "null";
    if (typeof url == "undefined" || null == url) {
        url = window.location.href;
    }
    var regex = /.*\:\/\/([^\/]*).*/;
    var match = url.match(regex);
    if (typeof match != "undefined" && null != match) {
        host = match[1];
    }
    return host;
}